<?php

namespace app\home\service;

use app\common\helpers\PayHelper;
use app\home\model\UserOrder;
use think\Config;

class HuaweiNew implements IPay
{
    private $channel = null;
    private $helper = null;

    public function __construct($params)
    {
        $this->channel = $params['channel'];
        $this->helper = PayHelper::getInstance($params['channel']);
    }

    public function createOrder($params)
    {
        if (empty($params['userid']) || empty($params['channel']) || empty($params['stroeid'])) {
            return false;
        }

        // 计费点映射关系
        $map_class = Config::get('MapPayClass');
        if(!isset($map_class[$params['channel']])){
            $this->helper->record('channel map not found');
            return false;
        }
        if(!isset($map_class[$params['channel']]['goods_id'])){
            $this->helper->record('goods_id config not found');
            return false;
        }
        if(!isset($map_class[$params['channel']]['goods_id'][$params['stroeid']])) {
            $this->helper->record('goods_id map not found');
            return false;
        }

        $orderModel = new UserOrder();
        $data = $orderModel->create_order_old($params['userid'], $params['channel'], $params['stroeid'], 5,null);
        $info = json_decode($data, true);
        $return['amount'] = $info['price'];
        $return['requestId'] = $info['orderid'];
        $return['ProductId'] = $map_class[$params['channel']]['goods_id'][$params['stroeid']];

        $data_Arr['code'] = 'success';
        $data_Arr['data'] = $return;

        return $data_Arr;
    }
    public function processNotify($params)
    {
        $split_str = str_repeat('-',80);
        $this->helper->record($split_str);
        $this->helper->record($params);

        if(empty($params['token']) || empty($params['productId']) || empty($params['order']) || empty($params['amount'])){
            $this->helper->record('some params empty');
            return 'params empty';
        }
        $purchaseToken = $params['token'];
        $productId = $params['productId'];
        $order = $params['order'];
        $amount = $params['amount'];
        $payOrderId = $params['payOrderId'];//三方订单


        // 验证订单
        $url = "https://orders-drcn.iap.hicloud.com/applications/purchases/tokens/verify";
        if($this->order_api($url,$purchaseToken,$productId) == false){
            $this->helper->record('verify fail');
            return 'verify fail';
        }
        $this->helper->record('verify ok');

        // 通知华为确认购买 不考虑是否通知成功
        $url = "https://orders-drcn.iap.hicloud.com/applications/v2/purchases/confirm";
        if($this->order_api($url,$purchaseToken,$productId) == false){
            $this->helper->record('confirm fail');
            // return 'confirm fail';
        }

        // 发货
        $time = date('Y-m-d H:i:s', time());
        $re = Db_connect_server_pay_domian($order, $amount/100, $time);
        if ($re != 'success') {
            $this->helper->record('delivery fail');
            return 'delivery fail';
        }

        return 'success';
    }

    protected function order_api($url,$purchaseToken,$productId){

        $data = array('purchaseToken'=>$purchaseToken,'productId'=>$productId);

        $basic = base64_encode('APPAT:'.$this->get_token());
        $curl_res = $this->helper->curl_post($url,$data,'json',$basic);
        $this->helper->record('curl:'.$curl_res);
        if(!$curl_res){
            return false;
        }
        $response_array = json_decode($curl_res,true);
        if($response_array['responseCode'] != 0){
            return false;
        }

        return true;
    }

    //"client_id":"213371913219605504",
    //"client_secret":"CD18373FEAED1FBA9EFBC9167392A7F2EC132AE9BAD001C9F984D2F72A3BFBBB",
    public function get_token(){
        $url = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
        $data['grant_type'] = 'client_credentials';
        $data['client_id'] = '100145873';
        $data['client_secret'] = '4f750edde9892642072620c88bd33649';

        $json = $this->helper->curl_post($url,$data);
        $this->helper->record($json);
        if($json == false){
            return false;
        }

        $res = json_decode($json,true);
        if(!isset($res['access_token'])){
            $this->helper->record('token fail '.$json);
            return false;
        }

        return $res['access_token'];
    }

}
